package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/18 13:51
 * @description：https://leetcode-cn.com/problems/simple-bank-system/
 */
public class 简易银行系统 {
    @Test
    public void test() {
    }


    class Bank {
        long[] balance;

        public Bank(long[] balance) {
            this.balance = balance;
        }

        public boolean transfer(int account1, int account2, long money) {
            if (!isValid(account1) || !isValid(account2)) {
                return false;
            }
            long balance1 = balance[account1 - 1];
            if (balance1 < money) {
                return false;
            }
            balance[account1 - 1] -= money;
            balance[account2 - 1] += money;
            return true;
        }

        public boolean deposit(int account, long money) {
            if (!isValid(account)) return false;
            balance[account - 1] += money;
            return true;
        }

        public boolean withdraw(int account, long money) {
            if (!isValid(account)) return false;
            if (balance[account - 1] < money) {
                return false;
            }
            balance[account - 1] -= money;
            return true;
        }

        public boolean isValid(int account) {
            return balance != null && balance.length > 0 && account > 0 && account <= balance.length;
        }
    }

/**
 * Your Bank object will be instantiated and called as such:
 * Bank obj = new Bank(balance);
 * boolean param_1 = obj.transfer(account1,account2,money);
 * boolean param_2 = obj.deposit(account,money);
 * boolean param_3 = obj.withdraw(account,money);
 */
}
